package com.zcb.myrxjavathreecache.presenter;

import android.media.Image;
import android.util.Log;

import com.zcb.myrxjavathreecache.modle.ImageBean;
import com.zcb.myrxjavathreecache.modle.ImageEntity;
import com.zcb.myrxjavathreecache.tools.api.ApiService;
import com.zcb.myrxjavathreecache.tools.api.HttpResultSubscriber;
import com.zcb.myrxjavathreecache.tools.api.ServiceFactory;
import com.zcb.myrxjavathreecache.tools.cache.CacheManager;
import com.zcb.myrxjavathreecache.tools.cache.NetworkCache;
import com.zcb.myrxjavathreecache.view.MyView;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/3/13.
 */

public class ImagePresenterImpl implements ImagePresenter {
    private MyView myView;

    public ImagePresenterImpl(MyView myView) {
        this.myView=myView;
    }

    @Override
    public List<ImageEntity> loadImgList(int pageSize ,int pageNum) {
        final String url = "http://gank.io/api/data/%E7%A6%8F%E5%88%A9/" + 30 + "/" + 1;

        NetworkCache<ImageEntity> networkCache=new NetworkCache<ImageEntity>() {
            @Override
            public Observable<ImageEntity> get(String key, Class<ImageEntity> cls) {

                Observable observable = ServiceFactory.getInstance()
                        .createService(ApiService.class).getImages(30,1)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io());
                return observable;
            }
        };

        Observable<ImageEntity> observable= CacheManager.getInstance().load(url,ImageEntity.class,networkCache);

        observable.map(new Func1<ImageEntity, List<ImageBean>>() {
            @Override
            public List<ImageBean> call(ImageEntity imageEntity) {
                List<ImageEntity.Image> imageList=imageEntity.results;
                List<ImageBean> list=new ArrayList<ImageBean>(imageList.size());
                for (ImageEntity.Image image : imageList){
                    ImageBean imagebean=new ImageBean();
                    imagebean.url=image.url;
                    imagebean.desc=image.desc;
                    list.add(imagebean);
                }
                return list;
            }
        })
        .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<ImageBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<ImageBean> imageBeens) {
//                        for(ImageBean bean : imageBeen){
//                            Log.i("zcb",bean.url+" "+bean.desc);
//                        }
                        myView.showImgs(imageBeens);

                    }
                });

//                Observable observable=ServiceFactory.getInstance()
//                        .createService(ApiService.class).getImages(30,1);
//        observable.subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new HttpResultSubscriber<ImageEntity>() {
//                    @Override
//                    public void onSuccess(ImageEntity imageEntity) {
//                        Log.i("zcb",imageEntity.results.size()+"长度");
//                    }
//
//                    @Override
//                    public void _onError(Throwable e) {
//
//                    }
//                });






























        return null;
    }
}
